package com.orangelife.service.propFeeRuleLinkMeter.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.github.pagehelper.PageHelper;
import com.orangelife.common.MybaitsGenericDaoSubLogic;
import com.orangelife.exception.AppBizException;
import com.orangelife.service.propFeeRuleLinkMeter.IPropFeeRuleLinkMeterService;
import com.orangelife.service.propFeeRuleLinkMeter.model.PropFeeRuleLinkMeterDto;
import com.orangelife.setting.constant.CodeConst;
import com.orangelife.setting.constant.InfoConst;
import com.orangelife.setting.constant.TypeConst;
import com.orangelife.utils.IdUtil;
import com.orangelife.utils.StringUtils;

/*****************************************************************
 * Copyright(C) ORANGELIFE Corporation CodeConst.BIS_CODE_68001015. All Rights
 * Reserved.
 *****************************************************************/
/*
 * ============================================================== 功能名
 * PropFeeRuleLinkMeter类的 service层的增删改查
 * 
 * @author @author zhr
 * 
 * @version 1.1.0
 * 
 * @date 2016-07-25 10:46:56
 * ==============================================================
 */
@Service("propFeeRuleLinkMeterService")
public class PropFeeRuleLinkMeterServiceImpl extends MybaitsGenericDaoSubLogic
		implements IPropFeeRuleLinkMeterService {
	private static Logger logger = Logger
			.getLogger(PropFeeRuleLinkMeterServiceImpl.class);

	public static Logger getLogger() {
		return logger;
	}

	public static void setLogger(Logger logger) {
		PropFeeRuleLinkMeterServiceImpl.logger = logger;
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int addPropFeeRuleLinkMeter(
			PropFeeRuleLinkMeterDto propFeeRuleLinkMeter)
			throws AppBizException {

		Assert.notNull(propFeeRuleLinkMeter);
		/************************************************
		 * 判断输入参数的长度以及是否能为空
		 ************************************************/

		propFeeRuleLinkMeter.setLinkID(IdUtil.getuuId());
		propFeeRuleLinkMeter.setItemStat(TypeConst.FLAG_ON);
		propFeeRuleLinkMeter.setTscreate(new Date());
		propFeeRuleLinkMeter.setTsedit(new Date());
		return this.getSqlSession().insert(
				"PropFeeRuleLinkMeterMapper.insertSelective",
				propFeeRuleLinkMeter);
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int editPropFeeRuleLinkMeter(
			PropFeeRuleLinkMeterDto propFeeRuleLinkMeter)
			throws AppBizException {

		Assert.notNull(propFeeRuleLinkMeter);
		/************************************************
		 * 判断输入参数的长度以及是否能为空
		 ************************************************/
		if (StringUtils.isBlank(propFeeRuleLinkMeter.getLinkID())) {
			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}
		PropFeeRuleLinkMeterDto oldPropFeeRuleLinkMeter = this
				.findPropFeeRuleLinkMeterByID(propFeeRuleLinkMeter.getLinkID());

		if (oldPropFeeRuleLinkMeter == null) {
			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}
		propFeeRuleLinkMeter.setTsedit(new Date());
		return this.getSqlSession().update(
				"PropFeeRuleLinkMeterMapper.updateByPrimaryKeySelective",
				propFeeRuleLinkMeter);
	}

	@Override
	public PropFeeRuleLinkMeterDto findPropFeeRuleLinkMeterByID(
			String propFeeRuleLinkMeterID) throws AppBizException {
		if (StringUtils.isBlank(propFeeRuleLinkMeterID)) {

			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}

		/************************************************
		 * 判断实体是否存在
		 ************************************************/
		PropFeeRuleLinkMeterDto oldPropFeeRuleLinkMeter = this.getSqlSession()
				.selectOne(
						"PropFeeRuleLinkMeterSearchMapper.selectByPrimaryKey",
						propFeeRuleLinkMeterID);
		if (oldPropFeeRuleLinkMeter == null) {
			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}

		if (TypeConst.FLAG_OFF.equals(oldPropFeeRuleLinkMeter.getItemStat())) {

			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}
		return oldPropFeeRuleLinkMeter;
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int deletePropFeeRuleLinkMeterByLogic(String propFeeRuleLinkMeterID)
			throws AppBizException {

		if (StringUtils.isBlank(propFeeRuleLinkMeterID)) {
			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}
		/************************************************
		 * 判断实体是否存在
		 ************************************************/
		PropFeeRuleLinkMeterDto oldPropFeeRuleLinkMeter = this
				.findPropFeeRuleLinkMeterByID(propFeeRuleLinkMeterID);

		oldPropFeeRuleLinkMeter.setItemStat(TypeConst.FLAG_OFF);
		oldPropFeeRuleLinkMeter.setTsedit(new Date());
		return this.getSqlSession().update(
				"PropFeeRuleLinkMeterMapper.updateByPrimaryKeySelective",
				oldPropFeeRuleLinkMeter);
	}

	@Override
	public List<Map<String, Object>> findPropFeeRuleLinkMeterByPage(
			String tsCreate, String search, int pageNo, int pageSize)
			throws AppBizException {

		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("tsCreate", tsCreate);
		map.put("search", search);
		PageHelper.startPage(pageNo, pageSize);
		List<Map<String, Object>> list = this.getSqlSession().selectList(
				"PropFeeRuleLinkMeterSearchMapper.findAllPropFeeRuleLinkMeter",
				map);
		return list;
	}

	@Override
	public List<PropFeeRuleLinkMeterDto> getPropFeeRuleLinkMeterByPage(
			String tsCreate, String search, int pageNo, int pageSize)
			throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("tsCreate", tsCreate);
		map.put("search", search);
		PageHelper.startPage(pageNo, pageSize);
		List<PropFeeRuleLinkMeterDto> list = this
				.getSqlSession()
				.selectList(
						"PropFeeRuleLinkMeterSearchMapper.getAllPropFeeRuleLinkMeterDto",
						map);
		return list;
	}

	@Override
	public List<Map<String, Object>> selectPropFeeRuleLinkMeterByRelevantID(
			String linkID, String ruleID, String commID, String meterID)
			throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("linkID", linkID);
		map.put("ruleID", ruleID);
		map.put("commID", commID);
		map.put("meterID", meterID);

		List<Map<String, Object>> list = this
				.getSqlSession()
				.selectList(
						"PropFeeRuleLinkMeterSearchMapper.selectPropFeeRuleLinkMeterDtoyRelevantID",
						map);
		return list;
	}

	@Override
	public List<PropFeeRuleLinkMeterDto> findPropFeeRuleLinkMeterByRelevantID(
			String linkID, String ruleID, String commID, String meterID)
			throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("linkID", linkID);
		map.put("ruleID", ruleID);
		map.put("commID", commID);
		map.put("meterID", meterID);

		List<PropFeeRuleLinkMeterDto> list = this
				.getSqlSession()
				.selectList(
						"PropFeeRuleLinkMeterSearchMapper.findPropFeeRuleLinkMeterDtoByRelevantID",
						map);
		return list;
	}

	@Override
	public List<PropFeeRuleLinkMeterDto> bauthfindPropFeeRuleLinkMeter(
			List<String> propFeeRuleLinkMeterIDs) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		List<PropFeeRuleLinkMeterDto> list = this
				.getSqlSession()
				.selectList(
						"PropFeeRuleLinkMeterSearchMapper.batchFindPropFeeRuleLinkMeter",
						propFeeRuleLinkMeterIDs);
		return list;
	}

	@Override
	public List<Map<String, Object>> bauthfindPropFeeRuleLinkMeterByMap(
			Map<String, Object> propFeeRuleLinkMeter) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		List<Map<String, Object>> list = this
				.getSqlSession()
				.selectList(
						"PropFeeRuleLinkMeterSearchMapper.batchFindPropFeeRuleLinkMeterByMap",
						propFeeRuleLinkMeter);
		return list;
	}

	@Override
	public void clearCache() throws AppBizException {
		System.out.println("清除缓存成功");
	}

}